package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: zhk
 * @since: 2025/7/3
 */
@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService  {
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private WmMaterialMapper WmMaterialMapper;
    @Override
    public WmMaterial uploadPicture(MultipartFile multipartFile)  {
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        WmMaterial result = WmMaterial.builder()
                .userId(WmThreadLocalUtils.getUser().getId())
                .url(fileId)
                .type((short)0)
                .isCollection((short)0)
                .createdTime(new Date())
                .build();
        save(result);
        return result;
    }

    @Override
    public PageResponseResult lists(WmMaterialDto wmMaterialDto) {

        wmMaterialDto.checkParam();
        IPage<WmMaterial> page = new Page(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null != wmMaterialDto.getIsCollection() && 1 == wmMaterialDto.getIsCollection(),WmMaterial::getIsCollection,wmMaterialDto.getIsCollection())
                .eq(WmMaterial::getUserId,WmThreadLocalUtils.getUser().getId())
                .orderByDesc(WmMaterial::getCreatedTime);
        page(page,lqw);
        PageResponseResult pageResponseResult = new PageResponseResult((int)page.getPages(),(int)page.getSize(),(int)page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;


    }
}
